package com.josh.behavior.predict;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

public class GetPopuSalesChart {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		GetPopuSalesChart gChart = new GetPopuSalesChart();
		gChart.popuSalesChart("E:/data/WeiBoData1.txt",
				"E:/data/ProductSlaes.txt", "E:/popuSalesChart.jpg");
	}

	public DefaultCategoryDataset getDataset(String popuPath, String salesPath)
			throws IOException {

		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		String series = "Sales Trend"; // series指的就是报表里的那条数据线

		// 获取产品关注度
		ArrayList<Double> popularities = new ArrayList<Double>();
		GetPopularity gPopu = new GetPopularity();
		popularities = gPopu.getPopu(popuPath, 5);

		// 获取产品销量
		ArrayList<Integer> productSales = new ArrayList<Integer>();
		GetSales pSales = new GetSales();
		productSales = pSales.getProductSales(salesPath, 0);

		// 为X轴赋值
		String[] timeValue = new String[5];
		for (int i = 0; i < 5; i++) {
			timeValue[i] = productSales.get(i).toString();
		}

		// 为Y轴添加数据值
		for (int i = 0; i < 5; i++) {
			// 值 哪条线 对应的横轴
			linedataset.addValue(popularities.get(i), series, timeValue[i]);
		}

		return linedataset;
	}

	public void popuSalesChart(String popuPath, String salesPath, String imgPath) {

		try {
			// 定义图标对象
			JFreeChart chart = ChartFactory.createLineChart(
					"Products Sales-Popularity",// 报表题目，字符串类型
					"Sales", // 横轴
					"Popularity", // 纵轴
					this.getDataset(popuPath, salesPath), // 获得数据集
					PlotOrientation.VERTICAL, // 图标方向垂直
					true, // 显示图例
					false, // 不用生成工具
					false // 不用生成URL地址
					);
			// 生成图形
			CategoryPlot plot = chart.getCategoryPlot();
			// 图像属性部分
			plot.setBackgroundPaint(Color.white);
			plot.setDomainGridlinesVisible(true); // 设置背景网格线是否可见
			plot.setDomainGridlinePaint(Color.BLACK); // 设置背景网格线颜色
			plot.setRangeGridlinePaint(Color.GRAY);
			plot.setNoDataMessage("No data");// 没有数据时显示的文字说明。
			// 数据轴属性部分
			NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
			rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			rangeAxis.setAutoRangeIncludesZero(true); // 自动生成
			rangeAxis.setUpperMargin(0.20);
			rangeAxis.setLabelAngle(Math.PI / 2.0);
			rangeAxis.setAutoRange(false);
			// 数据渲染部分 主要是对折线做操作
			LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
					.getRenderer();
			renderer.setBaseItemLabelsVisible(true);
			renderer.setSeriesPaint(0, Color.black); // 设置折线的颜色
			renderer.setBaseShapesFilled(true);
			renderer.setBaseItemLabelsVisible(true);
			renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
					ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
			renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

			renderer.setBaseItemLabelFont(new Font("Dialog", 1, 14)); // 设置提示折点数据形状
			plot.setRenderer(renderer);
			// 创建文件输出流
			File fos_jpg = new File(imgPath);
			// 输出到哪个输出流 // 统计图表对象// 高 // 宽
			ChartUtilities.saveChartAsJPEG(fos_jpg, chart, 500, 400);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
